import { desktopCapturer, ipcRenderer, contextBridge } from "electron";
// 分享屏幕或窗口
window.navigator.mediaDevices.getDisplayMedia = () => {
  return new Promise(async (resolve, reject) => {
    try {
      const sources = await desktopCapturer.getSources({ types: ["screen"] });
      let mandatory = {
        mandatory: {
          chromeMediaSource: "desktop",
          chromeMediaSourceId: sources[0].id,
        },
      }
      const stream = await window.navigator.mediaDevices.getUserMedia({
        audio: true,
        video: mandatory as MediaTrackConstraints,
        /*
         aspectRatio?: ConstrainDouble;
    autoGainControl?: ConstrainBoolean;
    channelCount?: ConstrainULong;
    deviceId?: ConstrainDOMString;
    displaySurface?: ConstrainDOMString;
    echoCancellation?: ConstrainBoolean;
    facingMode?: ConstrainDOMString;
    frameRate?: ConstrainDouble;
    groupId?: ConstrainDOMString;
    height?: ConstrainULong;
    noiseSuppression?: ConstrainBoolean;
    sampleRate?: ConstrainULong;
    sampleSize?: ConstrainULong;
    width?: ConstrainULong;
        */
      });
      resolve(stream);
    } catch (err) {
      reject(err);
    }
  });
};
// (window as any).shareSourceList = [];
// // (window as any).shareSourceList = [];
ipcRenderer.on("SEND_SCREEN_SHARE_SOURCES", async (event, sources) => {
  console.log(sources);
  // const stream = await window.navigator.mediaDevices.getUserMedia({
  //   audio: true,
  //   video: {
  //     advanced: [
  //       {
  //         deviceId: '1'
  //       },
  //     ]
  //   },

  // getUserMedia(
  //   {
  //     audio: false,
  //     video: {
  //       mandatory: {
  //         chromeMediaSource: "desktop",
  //         chromeMediaSourceId: sources[4].id,
  //       },
  //     },
  //   },
  //   function (stream) {
  //     console.log(stream); // stream就是媒体流
  //   }
  // );

  // const rtcCloud = TRTCCloud.getTRTCShareInstance();

  // const screenAndWindows = rtcCloud.getScreenCaptureSources(320, 180, 32, 32);

  // const selectedScreenOrWindow = screenAndWindows[1];

  // const selectRect = new Rect(0, 0, 0, 0);

  // const captureProperty = new TRTCScreenCaptureProperty(
  //   true, // enable capture mouse
  //   true, // enable highlight
  //   true, // enable high performance
  //   0, // default highlight color
  //   0, // default highlight width
  //   false // disable capture child window
  // );

  // rtcCloud.selectScreenCaptureTarget(
  //   selectedScreenOrWindow,
  //   selectRect,
  //   captureProperty
  // );

  // const screenShareEncParam = new TRTCVideoEncParam(
  //   TRTCVideoResolution.TRTCVideoResolution_1280_720,
  //   TRTCVideoResolutionMode.TRTCVideoResolutionModeLandscape,
  //   15,
  //   1600,
  //   0,
  //   true,
  // );
  // rtcCloud.startScreenCapture(
  //   document.getElementById('video'), // HTML Element
  //   TRTCVideoStreamType.TRTCVideoStreamTypeSub,
  //   screenShareEncParam,
  // );

  // console.log(stream);
  // const selectContainer = window.document.getElementById('screen-share-select')!;
  // console.log(sources);
  // (window as any).shareSourceList = sources;
  // sources.forEach((obj: any) => {
  //   const optionElement = document.createElement('option');
  //   optionElement.innerText = `${obj.name}`;
  //   selectContainer.appendChild(optionElement);
  // });
});
// function getUserMedia(constrains, success, error) {
//   if (navigator.mediaDevices.getUserMedia) {
//     //最新标准API
//     navigator.mediaDevices.getUserMedia(constrains).then(success).catch(error);
//   } else if (navigator.webkitGetUserMedia) {
//     //webkit内核浏览器
//     navigator.webkitGetUserMedia(constrains).then(success).catch(error);
//   } else if (navigator.mozGetUserMedia) {
//     //Firefox浏览器
//     navagator.mozGetUserMedia(constrains).then(success).catch(error);
//   } else if (navigator.getUserMedia) {
//     //旧版API
//     navigator.getUserMedia(constrains).then(success).catch(error);
//   }
// }

console.log(1111);
